package com.heima.behavior.controller;

import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.UserThreadLocalUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/behavior")
public class BehaviorController {


    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private IApBehaviorEntryService apBehaviorEntryService;

    /**
     * 回显当前登录人对文章的行为
     *
     * @param dto
     * @return
     */
    @PostMapping("/load_article_behavior")
    @ApiOperation(value = "loadBehavior", notes = "回显当前登录人对文章的行为")
    @ApiModelProperty(name = "dto", dataType = "BehaviorDto", required = true)
    public ResponseResult loadBehavior(@RequestBody BehaviorDto dto) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
//        dto.setUserId(user.getUserId());
//        return apBehaviorEntryService.loadArticleBehavior(dto);
//        {
//            "isfollow": true,
//                "islike": true,
//                "isunlike": false,
//                "iscollection": true
//        }
        Map<String, Boolean> map = new HashMap();

        map.put("isfollow", redisTemplate.hasKey("saveFollow_" + user.getUserId() + "_" + dto.getAuthorId() + "_" + dto.getArticleId() + "_" + dto.getEquipmentId()));
        map.put("islike", redisTemplate.hasKey("saveLikes_" + user.getUserId() + "_null_" + dto.getArticleId() + "_" + dto.getEquipmentId()));
        map.put("isunlike", redisTemplate.hasKey("saveUnlikes_" + user.getUserId() + "_null_" + dto.getArticleId() + "_" + dto.getEquipmentId()));
        map.put("iscollection", redisTemplate.hasKey("saveCollection_" + user.getUserId() + "_null_" + dto.getArticleId() + "_" + dto.getEquipmentId()));

        return ResponseResult.okResult(map);

    }
}
